<?php

namespace TpAmis\util;

class AmisConfig
{
    /**
     * TpAmis相关设置
     */
    static $config = [

        // +----------------------------------------------------------------------
        // | 表格相关设置
        // +----------------------------------------------------------------------
        'upload_api' => "/erp/amisupload/upload",

        "start_chunk_api" => "/erp/amisupload/upload_chunk_start",
        "chunk_api" => "/erp/amisupload/upload_chunk",
        "finish_chunk_api" => "/erp/amisupload/upload_chunk_finish",
        "chunk_size" => 1024 * 1024 * 2,
        "concurrency" => 3,
        'accept' => '.xls,.xlsx,.csv,.doc,.docx,.ppt,.pptx,.pdf,.txt,.rar,.zip',
    ];

    static $view;
    static $assign;
    static function setConfig(array $config = [])
    {
        static::$config = self::array_override(static::$config, $config);
    }

    /**
     * 将第一个多维数组的参数覆盖到第二个多维数组中
     * @param array $defaults 第二个多维数组，被覆盖
     * @param array $overrides 第一个多维数组，用于覆盖
     * @return array 覆盖后的数组
     */
    private static function array_override(array $defaults, array $overrides)
    {
        foreach ($overrides as $key => $value) {
            if (array_key_exists($key, $defaults)) {
                if (is_array($value) && is_array($defaults[$key])) {
                    // 递归处理子数组
                    $defaults[$key] = self::array_override($value, $defaults[$key]);
                } else {
                    // 覆盖当前键的值
                    $defaults[$key] = $value;
                }
            } else {
                // 添加新的键值对
                $defaults[$key] = $value;
            }
        }
        return $defaults;
    }

    static function getTpl()
    {
        return dirname(__DIR__) . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . 'tpl.html';
    }

    /**
     * 获取配置
     * @param string $name
     * @return mixed
     */
    static function get($name = '')
    {
        if ($name == '') {
            return static::$config;
        }
        //php判断$name中是否有字符串"."
        if (strpos($name, '.') !== false) {
            return static::getNestedValue($name);
        }

        return static::$config[$name];
    }
    /**
     * 设置配置
     * @param $name
     * @param $value
     */
    static function set($name, $value = '')
    {
        if (is_array($name)) {
            static::$config = array_merge(static::$config, $name);
        } else {
            if (strpos($name, '.') !== false) {
                static::setNestedValue($name, $value);
            } else {
                static::$config[$name] = $value;
            }
        }
    }
    /**
     * 获取二级配置
     * 使用示例：
     * echo getNestedValue( "database.host"); // 输出：127.0.0.1
     * echo getNestedValue(  "app.version"); // 输出：1.0.0
     * @param $config
     * @param $path
     */
    static private function getNestedValue($path)
    {
        $keys = explode('.', $path);
        $value = static::$config;
        foreach ($keys as $key) {
            if (is_array($value) && array_key_exists($key, $value)) {
                $value = $value[$key];
            } else {
                return null; // 如果键不存在或者已经不是数组，则返回null
            }
        }

        return $value;
    }
    /**
     * 设置多个配置
     * @param $name
     * @param $value
     */
    static private function setNestedValue($name, $value)
    {
        $keys = explode('.', $name);
        $config = &static::$config;
        while (count($keys) > 1) {
            $key = array_shift($keys);
            if (!isset($config[$key]) || !is_array($config[$key])) {
                $config[$key] = [];
            }
            $config = &$config[$key];
        }
        $config[array_shift($keys)] = $value;
    }
}
